import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.exceptions.NotEnoughFundException;
import com.acme.bankapp.exceptions.OverDraftLimitExceededException;
import com.acme.bankapp.service.bank.BankDataLoaderService;
import com.acme.bankapp.server.BankServer;
import com.acme.bankapp.service.bank.BankService;

import java.io.IOException;

public class BankApplication {
    private static String bankFile = "file.txt";

    public static void main(String[] args) {
        try {
            Bank bank = loadBank(args);
            BankService.modifyBank(bank, 0);
            BankService.printData(bank);
            BankDataLoaderService.saveBank(bank, bankFile);
            BankServer bankServer = new BankServer();
            bankServer.startServer(bank);
        } catch ( OverDraftLimitExceededException e ) {
            System.out.println("OverDraft limit exceeded, maximum amount available is " + e.getMaxAmount());
        } catch ( IllegalArgumentException | NotEnoughFundException e ) {
            System.out.println("Balance cannot be lower than 0");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Bank loadBank(String[] args) {
        Bank bank = null;
        for( int i = 0; i < args.length; i++ ) {
            if ( "-loadfeed".equals(args[i]) ) {
                bankFile = args[i+1];
                bank = BankDataLoaderService.loadBank(bankFile);
            }
        }
        return bank;
    }
}